package sample.jdbc.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.springframework.stereotype.Service;
import sample.jdbc.service.IMybatisOriginService;
import sample.jdbc.vo.TestVo;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 原始的Mybatis调用方法。
 */
@Service
public class MybatisOriginServiceImpl implements IMybatisOriginService {

    public void select() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 构建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        Map<String,Object> parameters = new HashMap<String,Object>();
        parameters.put("testId","1");
        TestVo test = sqlSession.selectOne("sample.jdbc.mapper.TestMapper.getTestById",parameters);
        if(test!=null)
          System.out.println("test:"+test.getTestId());
    }
}
